<?php


namespace app\admin\controller;


use QL\QueryList;
use think\facade\Db;

class Collect
{
    private $data = '';

    /**
     * 宅男平台数据抓取
     * @return \think\response\Json
     */
    public function searchSourceByNet()
    {
        try {
            $id          = request()->param('id', 1);
            $baseUrl     = 'http://zhainanx6.com';
            $ql          = QueryList::get('http://zhainanx6.com/index.php/vod/detail/id/' . $id . '.html');
            $rt          = [];
            $rt['title'] = $ql->find('.title:eq(0)')->text();
            $rt['cover'] = $ql->find('img.lazyload:eq(0)')->attr('data-original');
            $rt['type']  = $ql->find('p.data a')->text();
            $locationUrl = $ql->find('a.btn-primary:eq(0)')->attr('href');
            if (empty($locationUrl)) {
                throw new \Exception('采集视频的地址不存在！');
            }
            $locationUrl = $baseUrl . $locationUrl;
            $ql          = QueryList::get($locationUrl);
            $tempStr     = $ql->find('script')->text();
            preg_match('/"url":"(.*)","url_next"/', $tempStr, $match);
            $rt['video']       = stripslashes($match[1]);
            $rt['location']    = '';
            $rt['source_id']   = $id;
            $rt['click_num']   = '0';
            $rt['is_like']     = '0';
            $rt['create_time'] = time();
            $res               = Db::name('video')->save($rt);
            if (!$res) {
                throw new \Exception('数据插入失败！');
            }
        } catch (\Exception $exception) {
            return json(array('status' => 400, 'message' => '请求失败！', 'data' => []));
        }
        return $this->responseData();
    }

    /**
     * 久久日平台数据抓取
     * @return \think\response\Json
     */
    public function jiujiuriPage()
    {
        try {
            $page = request()->param('page', 1);

            $ql         = QueryList::get('http://25u51.cnzu95.com:6033/lmy/10-' . $page . '.html');
            $rt         = [];
            $length     = $ql->find('.stui-vodlist__box')->length();
            $insertData = [];
            $ids        = [];
            for ($i = 0; $i < $length; $i++) {
                //@todo  不同类型记得切换
                $rt['type']  = '自拍偷拍';
                $rt['cover'] = $ql->find('.stui-vodlist__box:eq(' . $i . ') .stui-vodlist__thumb')->attr('data-original');
                $rt['title'] = $ql->find('.stui-vodlist__box:eq(' . $i . ') .stui-vodlist__detail')->text();
                $locationUrl = $ql->find('.stui-vodlist__box:eq(' . $i . ') .stui-vodlist__detail .text-overflow a')->attr('href');
                if (empty($locationUrl)) {
                    continue;
                }
                $tempStr   = explode('/', $locationUrl)[2];
                $source_id = substr($tempStr, 0, strpos($tempStr, '-'));
                $ids[]     = $source_id;

                /*$locationUrl = $baseUrl . $locationUrl;
                $ql          = QueryList::get($locationUrl);
                $tempStr     = $ql->find('script')->text();
                preg_match('/"url":"(.*)","url_next"/', $tempStr, $match);
                $rt['video']       = stripslashes($match[1]);
                if(empty($rt['video'])){
                    continue;
                }*/
                $rt['video']       = '';
                $rt['location']    = '';
                $rt['source_id']   = $source_id;
                $rt['click_num']   = '0';
                $rt['is_like']     = '0';
                $rt['create_time'] = time();
                //var_export($rt);exit;
                //Db::name('jiujiuri_video')->insert($rt);
                $insertData[] = $rt;
            }
            $int = Db::name('jiujiuri_video')->insertAll($insertData);
            if (!$int) {
                throw new \Exception('数据插入失败！');
            }
            $this->data = $ids;
        } catch (\Exception $exception) {
            return json(array('status' => 400, 'message' => $exception->getMessage(), 'data' => []));
        }
        return $this->responseData();
    }

    /**
     * 久久日平台数据抓取
     * @return \think\response\Json
     */
    public function jiujiuriById($id = 1)
    {
        try {
            $locationUrl = 'http://25u51.cnzu95.com:6033/spy/' . $id . '-1-1.html';
            $ql          = QueryList::get($locationUrl);
            $tempStr     = $ql->find('script')->text();
            preg_match('/"url":"(.*)","url_next"/', $tempStr, $match);
            $rt = [
                'video' => stripslashes($match[1])
            ];
            if (empty($rt['video'])) {
                throw new \Exception('视频播放信息不存在！');
            }
            $int = Db::name('jiujiuri_video')->where('source_id',$id)->update($rt);
            if (!$int) {
                throw new \Exception('数据插入失败！');
            }
        } catch (\Exception $exception) {
            return json(array('status' => 400, 'message' => $exception->getMessage(), 'data' => []));
        }
        return $this->responseData();
    }

    public function responseData()
    {
        return json([
            'status'  => 200,
            'message' => '请求成功！',
            'data'    => $this->data
        ]);
    }
}